package com.union.core.information.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.union.core.information.model.KnowledgeManage;
import com.union.core.information.service.KnowledgeManageService;
import com.union.core.organization.model.Personnel;
import com.union.core.sys.service.SysLogsService;
import com.union.core.sys.service.SysService;
/**
 * action类: 知识管理
 * filename: KnowledgeManageAction.java
 * project:  union
 * @company Hainan Jowe Technology Ltd
 * @author Harry Zhang
 * @create in 2012-11-05
 * @version 1.0
 */
public class KnowledgeManageAction extends ActionSupport {
	private static final long serialVersionUID = -2537150204040817898L;
	private SysService sysService = SysService.getInstance();
	//知识管理模块参数
	private int id;
	private String knowledgeTitle;
	private String knowledgeContent;
	private String releaseName;
	private String releaseTime;
	private int organizationId;
	private int personId;
	private String organizationName;
	private String editName;
	private String editTime;
	private String paraName;
	private String paraVal;
	private String orderBy;
	//时间查询
	private String startDate;

	private String endDate;
	//分页设置
	private int page=1;			//页码
	private int intRowCount;	//总行数
	private int intPageCount;	//总页数
	private int start;				//开始
	private int intPageSize=15;	//每页显示的行数
	private int intPage;		//页数变量
	
	//知识列表
	private List<KnowledgeManage> knowledgeManageList;

	//服务类
	private KnowledgeManageService knowledgeMS;
	//知识数据实例
	private KnowledgeManage knowledgeManage;
	//反馈信息
	private String operRes;
	//日志
	private SysLogsService sysLogsService;
	//权限控制
	private List<String> allPower;
	
	public KnowledgeManageAction(){
		knowledgeMS=new KnowledgeManageService();
		sysLogsService=new SysLogsService();
	}
	public String knowledgeIndex(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			lookFor();
			setQuerySession();
		return SUCCESS;
		}
	}
	public String pagelist(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			allPower=sysService.getSessionListener().getAllPower();
			getQuerySession();
			lookFor();
		return SUCCESS;
		}
	}
	public String knowledgeAddLoad(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			
			return SUCCESS;
		}
	}
	public String addKnowledge(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			Personnel personnel=sysService.getSessionListener().getPERSONNEL();
			if(personnel!=null){
				knowledgeManage=new KnowledgeManage();
				knowledgeManage.setOrganization_name(personnel.getOrganization_name());
				knowledgeManage.setRelease_name(personnel.getPersonnel_name());
				knowledgeManage.setKnowledge_title(knowledgeTitle);
				knowledgeManage.setKnowledge_content(knowledgeContent);
				knowledgeManage.setOrganization_id(personnel.getOrganization_id());
				knowledgeManage.setOrganization_name(personnel.getOrganization_name());
				knowledgeManage.setPerson_id(personnel.getPersonnel_id());
				knowledgeManage.setRelease_name(personnel.getPersonnel_name());
				boolean bl=knowledgeMS.addKnowledgeManage(knowledgeManage);
				if(bl){
					sysLogsService.addSysLogs("添加公会知识【"+knowledgeTitle+"】");
					operRes="恭喜！添加公会知识【"+knowledgeTitle+"】成功";
				}else{
					operRes="很遗憾！添加公会知识【"+knowledgeTitle+"】失败";
				}
			}else{
				knowledgeManage=new KnowledgeManage();
				knowledgeManage.setOrganization_name("系统");
				knowledgeManage.setRelease_name("system");
				knowledgeManage.setKnowledge_title(knowledgeTitle);
				knowledgeManage.setKnowledge_content(knowledgeContent);
				knowledgeManage.setOrganization_id(0);
				knowledgeManage.setOrganization_name("系统");
				knowledgeManage.setPerson_id(0);
				knowledgeManage.setRelease_name("system");
				boolean bl=knowledgeMS.addKnowledgeManage(knowledgeManage);
				if(bl){
					sysLogsService.addSysLogs("添加公会知识【"+knowledgeTitle+"】");
					operRes="恭喜！添加公会知识【"+knowledgeTitle+"】成功";
				}else{
					operRes="很遗憾！添加公会知识【"+knowledgeTitle+"】失败";
				}
			}
			return SUCCESS;
		}
	}

	public String queryKnowledge(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			
			knowledgeManage=knowledgeMS.queryById(id);
			id=0;
			start=0;
			intPageSize=200;
			lookFor();
		return SUCCESS;
		}
	}
	public String delKnowledge(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			knowledgeManage=knowledgeMS.queryById(id);
			boolean bl=knowledgeMS.delKnowledgeManage(id);
			if(bl){
				sysLogsService.addSysLogs("删除公会知识【"+knowledgeManage.getKnowledge_title()+"】");
				operRes="恭喜！删除公会知识【"+knowledgeManage.getKnowledge_title()+"】成功";
			}else{
				operRes="很遗憾！删除公会知识【"+knowledgeManage.getKnowledge_title()+"】失败";
			}
			return SUCCESS;
		}
	}
	public String editKnowledgeLoad(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			knowledgeManage=knowledgeMS.queryById(id);
			return SUCCESS;
		}
	}
	


	public String editKnowledge(){
		if(sysService.ifTimeOut()==false){
			return "TIMEOUT";//超时
		}else{
			Personnel personnel=sysService.getSessionListener().getPERSONNEL();
			
				knowledgeManage=knowledgeMS.queryById(id);
				knowledgeManage.setKnowledge_title(knowledgeTitle);
				knowledgeManage.setKnowledge_content(knowledgeContent);
			if(personnel!=null){
				knowledgeManage.setEdit_name(editName);
			}else{
				knowledgeManage.setEdit_name("system");
			}
			boolean bl=knowledgeMS.editKnowledge(knowledgeManage);
			if(bl){
				sysLogsService.addSysLogs("修改公会知识【"+knowledgeTitle+"】");
				operRes="恭喜！修改公会知识【"+knowledgeTitle+"】成功";
			}else{
				operRes="很遗憾！修改公会知识【"+knowledgeTitle+"】失败";
			}
			
		return SUCCESS;
		}
	}
	
	
	private void lookFor(){
		
		if("".equals(id))id = 0;
		if("".equals(knowledgeTitle))knowledgeTitle = null;
		if("".equals(knowledgeContent))knowledgeContent = null;
		if("".equals(startDate))startDate = null;
		if("".equals(endDate))endDate = null;
		if("".equals(releaseName))releaseName = null;
		if("".equals(releaseTime))releaseTime = null;
		if("".equals(organizationId))organizationId = 0;
		if("".equals(personId))personId = 0;
		if("".equals(organizationName))organizationName = null;
		
		if("".equals(paraName))paraName = null;
		if("".equals(paraVal))paraVal = null;
		if("".equals(orderBy))orderBy = null;
		
		if(page<1){
			intPage = 1;
			page=1;
		}else{
			intPage = page;
		}
		intRowCount  = knowledgeMS.queryKnowledgeManageListCount(id, knowledgeTitle, knowledgeContent, 
				releaseName, releaseTime, organizationId, personId, organizationName,startDate,endDate, paraName, paraVal);
		intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
		if(intPage > intPageCount) intPage = intPageCount;		
		if(intPageCount>0){
			start = (intPage-1) * intPageSize;
			knowledgeManageList=knowledgeMS.queryKnowledgeManageList(id, knowledgeTitle, knowledgeContent, 
					releaseName, releaseTime, organizationId, personId, organizationName, startDate,endDate,
					paraName, paraVal, orderBy, start, intPageSize);
		}
	}
	
	
	private void setQuerySession(){
		ServletActionContext.getRequest().getSession().setAttribute("id",id); 
		ServletActionContext.getRequest().getSession().setAttribute("knowledgeTitle",knowledgeTitle); 
		ServletActionContext.getRequest().getSession().setAttribute("knowledgeContent",knowledgeContent); 
		ServletActionContext.getRequest().getSession().setAttribute("releaseName",releaseName); 
		ServletActionContext.getRequest().getSession().setAttribute("releaseTime",releaseTime); 
		ServletActionContext.getRequest().getSession().setAttribute("organizationId",organizationId); 
		ServletActionContext.getRequest().getSession().setAttribute("personId",personId); 
		ServletActionContext.getRequest().getSession().setAttribute("organizationName",organizationName); 
		ServletActionContext.getRequest().getSession().setAttribute("startDate",startDate); 
		ServletActionContext.getRequest().getSession().setAttribute("endDate",endDate); 
		ServletActionContext.getRequest().getSession().setAttribute("paraName",paraName); 
		ServletActionContext.getRequest().getSession().setAttribute("paraVal",paraVal); 
		ServletActionContext.getRequest().getSession().setAttribute("orderBy",orderBy); 
	}
	
	private void getQuerySession(){
		id = (Integer)ServletActionContext.getRequest().getSession().getAttribute("id");
		knowledgeTitle = (String)ServletActionContext.getRequest().getSession().getAttribute("knowledgeTitle");
		knowledgeContent = (String)ServletActionContext.getRequest().getSession().getAttribute("knowledgeContent");
		releaseName = (String)ServletActionContext.getRequest().getSession().getAttribute("releaseName");
		releaseTime = (String)ServletActionContext.getRequest().getSession().getAttribute("releaseTime");
		organizationId = (Integer)ServletActionContext.getRequest().getSession().getAttribute("organizationId");
		personId = (Integer)ServletActionContext.getRequest().getSession().getAttribute("personId");
		organizationName = (String)ServletActionContext.getRequest().getSession().getAttribute("organizationName");
		startDate = (String)ServletActionContext.getRequest().getSession().getAttribute("startDate");
		endDate = (String)ServletActionContext.getRequest().getSession().getAttribute("endDate");
		paraName = (String)ServletActionContext.getRequest().getSession().getAttribute("paraName");
		paraVal = (String)ServletActionContext.getRequest().getSession().getAttribute("paraVal");
		orderBy = (String)ServletActionContext.getRequest().getSession().getAttribute("orderBy");
	}
	
	public List<KnowledgeManage> getKnowledgeManageList() {
		return knowledgeManageList;
	}
	public void setKnowledgeManageList(List<KnowledgeManage> knowledgeManageList) {
		this.knowledgeManageList = knowledgeManageList;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getIntPageCount() {
		return intPageCount;
	}
	public void setIntPageCount(int intPageCount) {
		this.intPageCount = intPageCount;
	}
	public int getIntRowCount() {
		return intRowCount;
	}
	public String getReleaseName() {
		return releaseName;
	}
	public void setReleaseName(String releaseName) {
		this.releaseName = releaseName;
	}
	public String getReleaseTime() {
		return releaseTime;
	}
	public void setReleaseTime(String releaseTime) {
		this.releaseTime = releaseTime;
	}
	public int getOrganizationId() {
		return organizationId;
	}
	public void setOrganizationId(int organizationId) {
		this.organizationId = organizationId;
	}
	public int getPersonId() {
		return personId;
	}
	public void setPersonId(int personId) {
		this.personId = personId;
	}
	public String getOrganizationName() {
		return organizationName;
	}
	public void setOrganizationName(String organizationName) {
		this.organizationName = organizationName;
	}
	public String getEditTime() {
		return editTime;
	}
	public void setEditTime(String editTime) {
		this.editTime = editTime;
	}
	public String getParaName() {
		return paraName;
	}
	public void setParaName(String paraName) {
		this.paraName = paraName;
	}
	public String getParaVal() {
		return paraVal;
	}
	public void setParaVal(String paraVal) {
		this.paraVal = paraVal;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getIntPageSize() {
		return intPageSize;
	}
	public void setIntPageSize(int intPageSize) {
		this.intPageSize = intPageSize;
	}
	public int getIntPage() {
		return intPage;
	}
	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}
	public KnowledgeManageService getKnowledgeMS() {
		return knowledgeMS;
	}
	public void setKnowledgeMS(KnowledgeManageService knowledgeMS) {
		this.knowledgeMS = knowledgeMS;
	}
	public void setIntRowCount(int intRowCount) {
		this.intRowCount = intRowCount;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getKnowledgeTitle() {
		return knowledgeTitle;
	}
	public void setKnowledgeTitle(String knowledgeTitle) {
		this.knowledgeTitle = knowledgeTitle;
	}
	public String getEditName() {
		return editName;
	}
	public void setEditName(String editName) {
		this.editName = editName;
	}
	public String getKnowledgeContent() {
		return knowledgeContent;
	}
	public void setKnowledgeContent(String knowledgeContent) {
		this.knowledgeContent = knowledgeContent;
	}
	public String getOperRes() {
		return operRes;
	}
	public void setOperRes(String operRes) {
		this.operRes = operRes;
	}
	public KnowledgeManage getKnowledgeManage() {
		return knowledgeManage;
	}
	public void setKnowledgeManage(KnowledgeManage knowledgeManage) {
		this.knowledgeManage = knowledgeManage;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public List<String> getAllPower() {
		return allPower;
	}
	public void setAllPower(List<String> allPower) {
		this.allPower = allPower;
	}
}
